package com.sky.orangehireserver.modules.job_seeker.controller;

import com.sky.orangehireserver.aspect.ApiOperationLog;
import com.sky.orangehireserver.modules.job_seeker.dto.SeekerLoginDTO;
import com.sky.orangehireserver.modules.job_seeker.dto.SeekerRegisterDTO;
import com.sky.orangehireserver.modules.job_seeker.dto.SeekerUpdateDTO;
import com.sky.orangehireserver.modules.job_seeker.service.SeekerAccountService;
import com.sky.orangehireserver.modules.job_seeker.vo.SeekerAccountVO;
import com.sky.orangehireserver.modules.job_seeker.vo.SeekerLoginVO;
import com.sky.orangehireserver.utils.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 求职者账号管理控制器
 * 
 * @author sky
 */
@Slf4j
@RestController
@RequestMapping("/seeker/account")
@RequiredArgsConstructor
@Tag(name = "求职者账号管理", description = "求职者账号相关接口")
public class SeekerAccountController {

    private final SeekerAccountService seekerAccountService;

    /**
     * 求职者注册
     * 
     * @param registerDTO 注册请求DTO
     * @return 用户信息VO
     */
    @PostMapping("/register")
    @Operation(summary = "求职者注册", description = "求职者用户注册")
    @ApiOperationLog(description = "求职者注册")
    public Response<SeekerAccountVO> register(@Valid @RequestBody SeekerRegisterDTO registerDTO) {
        SeekerAccountVO accountVO = seekerAccountService.register(registerDTO);
        return Response.success(accountVO);
    }

    /**
     * 求职者登录
     * 
     * @param loginDTO 登录请求DTO
     * @return 登录响应VO
     */
    @PostMapping("/login")
    @Operation(summary = "求职者登录", description = "求职者用户登录")
    @ApiOperationLog(description = "求职者登录")
    public Response<SeekerLoginVO> login(@Valid @RequestBody SeekerLoginDTO loginDTO) {
        SeekerLoginVO loginVO = seekerAccountService.login(loginDTO);
        return Response.success(loginVO);
    }

    /**
     * 获取当前用户信息
     * 
     * @param userId 用户ID（从请求头或上下文中获取）
     * @return 用户信息VO
     */
    @GetMapping("/profile")
    @Operation(summary = "获取当前用户信息", description = "获取当前登录用户的详细信息")
    @ApiOperationLog(description = "获取当前用户信息")
    public Response<SeekerAccountVO> getCurrentUserProfile(
            @Parameter(description = "用户ID", hidden = true) @RequestHeader(value = "X-User-Id", required = false) Long userId) {
        
        // TODO: 在实际项目中，用户ID应该从JWT token或用户上下文中获取
        if (userId == null) {
            throw new RuntimeException("用户ID不能为空，请在请求头中添加X-User-Id");
        }
        
        SeekerAccountVO accountVO = seekerAccountService.getSeekerById(userId);
        return Response.success(accountVO);
    }

    /**
     * 根据ID获取用户信息
     * 
     * @param id 用户ID
     * @return 用户信息VO
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取用户信息", description = "根据用户ID获取用户信息")
    @ApiOperationLog(description = "根据ID获取用户信息")
    public Response<SeekerAccountVO> getUserById(@Parameter(description = "用户ID") @PathVariable Long id) {
        SeekerAccountVO accountVO = seekerAccountService.getSeekerById(id);
        return Response.success(accountVO);
    }

    /**
     * 更新当前用户信息
     * 
     * @param userId 用户ID（从请求头或上下文中获取）
     * @param updateDTO 更新请求DTO
     * @return 用户信息VO
     */
    @PutMapping("/profile")
    @Operation(summary = "更新当前用户信息", description = "更新当前登录用户的信息")
    @ApiOperationLog(description = "更新当前用户信息")
    public Response<SeekerAccountVO> updateCurrentUserProfile(
            @Parameter(description = "用户ID", hidden = true) @RequestHeader(value = "X-User-Id", required = false) Long userId,
            @Valid @RequestBody SeekerUpdateDTO updateDTO) {
        
        if (userId == null) {
            throw new RuntimeException("用户ID不能为空，请在请求头中添加X-User-Id");
        }
        
        SeekerAccountVO accountVO = seekerAccountService.updateSeeker(userId, updateDTO);
        return Response.success(accountVO);
    }

    /**
     * 求职者退出登录
     * 
     * @param userId 用户ID（从请求头或上下文中获取）
     * @param authorization 授权头（包含访问令牌）
     * @return 操作结果
     */
    @PostMapping("/logout")
    @Operation(summary = "求职者退出登录", description = "求职者用户退出登录")
    @ApiOperationLog(description = "求职者退出登录")
    public Response<Void> logout(
            @Parameter(description = "用户ID", hidden = true) @RequestHeader(value = "X-User-Id", required = false) Long userId,
            @Parameter(description = "授权头") @RequestHeader(value = "Authorization", required = false) String authorization) {
        
        if (userId == null) {
            throw new RuntimeException("用户ID不能为空，请在请求头中添加X-User-Id");
        }
        
        // 提取token
        String token = null;
        if (authorization != null && authorization.startsWith("Bearer ")) {
            token = authorization.substring(7);
        }
        
        seekerAccountService.logout(userId, token);
        return Response.success();
    }

    /**
     * 注销当前用户账号
     * 
     * @param userId 用户ID（从请求头或上下文中获取）
     * @return 操作结果
     */
    @DeleteMapping("/profile")
    @Operation(summary = "注销当前用户账号", description = "永久删除当前用户账号")
    @ApiOperationLog(description = "注销当前用户账号")
    public Response<Void> deleteCurrentAccount(
            @Parameter(description = "用户ID", hidden = true) @RequestHeader(value = "X-User-Id", required = false) Long userId) {
        
        if (userId == null) {
            throw new RuntimeException("用户ID不能为空，请在请求头中添加X-User-Id");
        }
        
        seekerAccountService.deleteAccount(userId);
        return Response.success();
    }

    /**
     * 刷新访问令牌
     * 
     * @param authorization 授权头（包含当前访问令牌）
     * @return 新的登录响应VO
     */
    @PostMapping("/refresh-token")
    @Operation(summary = "刷新访问令牌", description = "使用当前令牌获取新的访问令牌")
    @ApiOperationLog(description = "刷新访问令牌")
    public Response<SeekerLoginVO> refreshToken(
            @Parameter(description = "授权头") @RequestHeader(value = "Authorization") String authorization) {
        
        // 提取token
        String token = null;
        if (authorization != null && authorization.startsWith("Bearer ")) {
            token = authorization.substring(7);
        }
        
        if (token == null) {
            throw new RuntimeException("访问令牌不能为空");
        }
        
        SeekerLoginVO loginVO = seekerAccountService.refreshToken(token);
        return Response.success(loginVO);
    }
}
